package com.car.loan.project.xr.controller;

import com.car.loan.common.exception.loan.LoanParameterException;
import com.car.loan.framework.aspectj.lang.annotation.DataStatus;
import com.car.loan.framework.aspectj.lang.annotation.Log;
import com.car.loan.framework.aspectj.lang.enums.BusinessType;
import com.car.loan.framework.web.domain.AjaxResult;
import com.car.loan.project.xr.domain.LoanStatus;
import com.car.loan.project.xr.service.ILoanAlbumService;
import com.car.loan.project.xr.service.ILoanCarInfoService;
import com.car.loan.project.xr.service.ILoanService;
import com.car.loan.project.xr.service.ILoanStatusService;
import com.car.loan.project.xr.util.bo.business.InsuranceInfoBo;
import com.car.loan.project.xr.util.bo.business.LoanAlbumDeleteBo;
import com.car.loan.project.xr.util.bo.business.LoanBaseInfo;
import com.car.loan.project.xr.util.enums.FileAuditStatus;
import com.car.loan.project.xr.util.enums.InsuranceInfoStatus;
import com.car.loan.project.xr.util.enums.LoanAlbumType;
import com.car.loan.project.xr.util.select.LoanBasePageParam;
import com.car.loan.project.xr.util.vo.CarInfoInputVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import java.util.Optional;
import javax.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author huangding
 * @description
 * @date 2019/12/8 10:33
 */
@RestController
@RequestMapping("xr/insurance/info")
@Api(value = "保险信息", tags = "车贷业务-保险信息")
public class LoanInsuranceInfoController {

    @Autowired
    private ILoanService loanService;

    @Autowired
    private ILoanStatusService loanStatusService;

    @Autowired
    private ILoanCarInfoService loanCarInfoService;

    @Autowired
    private ILoanAlbumService loanAlbumService;


    @PreAuthorize("@ss.hasPermi('insurance:info:list')")
    @GetMapping("list")
    @ApiOperation(value = "获取保险信息列表", response = LoanBaseInfo.class, responseContainer = "list")
    @DataStatus(loanStatusEnum = com.car.loan.common.enums.LoanStatusEnum.INSURANCE_INFO_STATUS)
    public AjaxResult applyList(@Valid LoanBasePageParam loanBasePageParam) {
        return AjaxResult.success(loanService.page(loanBasePageParam));
    }


    @PreAuthorize("@ss.hasPermi('insurance:info:query')")
    @GetMapping("{loanId}")
    @ApiOperation(value = "获取保险信息详情", response = CarInfoInputVo.class)
    public AjaxResult saveApply(@PathVariable Long loanId) {
        return AjaxResult.success(loanCarInfoService.getCarInfoInputVo(loanId));
    }


    @PreAuthorize("@ss.hasPermi('insurance:info:input')")
    @PostMapping("input")
    @Log(title = "保险信息录入", businessType = BusinessType.UPDATE)
    @ApiOperation(value = "保险信息录入")
    public AjaxResult audit(@RequestBody @Valid InsuranceInfoBo insuranceInfoBo) {
        return loanCarInfoService.insuranceInput(insuranceInfoBo);
    }

    @PreAuthorize("@ss.hasPermi('insurance:album:delete')")
    @DeleteMapping("album")
    @ApiOperation(value = "保险附件删除", notes = "已提交不可删除")
    @Log(title = "删除附件", businessType = BusinessType.DELETE)
    public AjaxResult deleteImages(@Valid @RequestBody LoanAlbumDeleteBo loanAlbumDeleteBo) {
        Optional<LoanStatus> optionalLoanStatus = loanStatusService
            .getByLoanId(loanAlbumDeleteBo.getLoanId());
        LoanStatus loanStatus = optionalLoanStatus
            .orElseThrow(() -> new LoanParameterException("该业务状态数据异常，请联系管理员"));
        if (InsuranceInfoStatus.INIT.status != loanStatus.getInsuranceInfoStatus()) {
            return AjaxResult.error("已提交不可删除");
        }
        if (null == loanAlbumDeleteBo.getType() || (
            loanAlbumDeleteBo.getType() != LoanAlbumType.CAR_INFO.type
                || loanAlbumDeleteBo.getType() != LoanAlbumType.INSURANCE.type)) {
            return AjaxResult.error("请传入对应证件类型");
        }
        loanAlbumService
            .deleteLoanAlbumByIdAndType(loanAlbumDeleteBo.getId(), loanAlbumDeleteBo.getType());
        return AjaxResult.success();
    }


    @PreAuthorize("@ss.hasPermi('insurance:info:withdraw')")
    @PutMapping("withdraw/{loanId}")
    @Log(title = "撤回保险信息录入", businessType = BusinessType.WITHDRAW)
    @ApiOperation(value = "撤回保险信息录入", notes = "档案信息已审核后不可撤回")
    public AjaxResult withdrawApply(@PathVariable Long loanId) {
        Optional<LoanStatus> optionalLoanStatus = loanStatusService.getByLoanId(loanId);
        LoanStatus loanStatus = optionalLoanStatus
            .orElseThrow(() -> new LoanParameterException("业务状态数据异常请联系管理员"));
        Long fileAuditStatus = loanStatus.getFileAuditStatus();
        if (FileAuditStatus.SUBMIT.status == fileAuditStatus) {
            return AjaxResult.error("档案信息已审核，不可撤回");
        }
        loanStatus.setInsuranceInfoStatus((long) InsuranceInfoStatus.INIT.status);
        loanStatusService.updateLoanStatus(loanStatus);
        return AjaxResult.success();
    }
}
